capture log close
clear
clear matrix
set mem 10000000
set more off
#delimit ;

*******************************************************************************************************;

*Ritter & Conrad APSR 2016;
*This .do performs the matching analyses reported in Table 1;

*******************************************************************************************************;

use RC_APSR_Data.dta, clear;
tsset adm0_adm1 date; 
sort cow year;
merge cow year using "Latent_Democracy.dta";
rename mean latent_democracy;
drop _merge; 
sort cow year;
merge cow year using "QoGData.dta";
drop _merge; 
tsset adm0_adm1 date; 

***************************************************

*Generate measures for repression and dissent;

gen dissentcount=etype1+etype2+etype3+etype4+etype5+etype6+etype8
	+escalation1+escalation2+escalation3+escalation4+escalation5+escalation6+escalation8; 
gen represscount=escalation7+repBINARYone+repBINARYtwo; 
drop etype1 etype2 etype3 etype4 etype5 etype6 etype7 etype8 etype9 etype10 
	escalation0 escalation1 escalation2 escalation3 escalation4 escalation5 escalation6 escalation7
	escalation8 escalation9 escalation10 etypeNEG9 repBINARYone repBINARYtwo repBINARYzero;
	
gen repressbinary=.;
replace repressbinary=1 if represscount>=1;
replace repressbinary=0 if represscount==0;

gen dissentbinary=.;
replace dissentbinary=1 if dissentcount>=1;
replace dissentbinary=0 if dissentcount==0;

*Generate log of rain;
gen lograin=ln(rainnonull_mean);
sum rainnonull_mean;
sum lograin;
replace lograin=0 if lograin==.;
sum lograin;

xtset adm0_adm1 date;

gen democracydissent=latent_democracy*dissentcount;
gen democracyrain=latent_democracy*lograin;
gen democracyfullrain=latent_democracy*rainnonull_mean;

gen raindeviations=rainnonull_mean-(annualrainsum/365);
sum raindeviations;

*Break lograin at 0 for treatment;
gen raintreatment=.;
replace raintreatment=0 if lograin<=0;
replace raintreatment=1 if lograin>0;
tab raintreatment;

*Pre-treatment covariates: urban, population, and wealth;
sum urban_mean;
sum pop_sum;
sum wdi_gdp;

*CEM Matching;
imb urban_mean pop_sum wdi_gdp, treatment(raintreatment);
cem urban_mean pop_sum wdi_gdp, tr(raintreatment);

***************************************************;

ivreg2 represscount urban_mean (dissentcount = lograin rainannualpct) [iweight=cem_weights], first;

*****;

ivreg2 represscount urban_mean (dissentcount = lograin rainannualpct) if latent_democracy<=0 [iweight=cem_weights], first;
	
*****;

ivreg2 represscount urban_mean (dissentcount = lograin rainannualpct) if latent_democracy>0 [iweight=cem_weights], first;
	
***************************************************;
